<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="车辆品牌管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">

            <el-form-item label="车辆品牌名称" prop="brandName">
                <el-input v-model="form.brandName" placeholder="请输入"/>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {VehicleBrandApi} from '@/api/user/VehicleBrandApi'
import {Common} from '@/utils/common'

export default {
    name: 'UserVehicleBrandUpdate',
    components: {},
    data() {
        return {
            form: {
                id: '',
                brandName: '',
            },
            rules: {

                brandName: [
                    {required: true, message: '请输入车辆品牌', trigger: 'blur'},
                    {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
                ],


            },
            dialogVisible: false
        }
    },
    created() {
    },
    methods: {
        // 显示组件
        async show(data) {
            const that = this

            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
                that.form.id = ""
            }

            console.log('1111',data)
            that.$nextTick(() => {
                if (data.id > 0) {
                    that.form = Common.assign(that.form, data)
                }

                console.log('2222',that.form)
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (!valid) return false

                if (that.form.id) {
                    VehicleBrandApi.edit(that.form).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                } else {
                    VehicleBrandApi.create(that.form).then(() => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>


